# coding: utf-8
# 知识点的生成，即NER结果的后处理

import csv
import os
import re

filenames_in1 = 'D:\\学科知识图谱项目\\NERresult\\移动通信原理与系统三级目录NER\\'  # 教材NER结果文件的地址
#filenames_in2 = 'C:\\Users\\StriveQueen\\PycharmProjects\\textrank_tfidf\\关键词提取\\课程关键词\\移动通信原理与系统关键词\\三级目录\\'    # 教材关键词结果文件的地址
filenames_out = 'C:\\Users\\StriveQueen\\Desktop\\合并后知识点\\移动通信原理与系统知识点\\'  # 知识点文件的地址
pathDir1 = os.listdir(filenames_in1)
#pathDir2 = os.listdir(filenames_in2)


def get_keywords(file_in, file):
    with open(file_in + file, 'r', encoding='utf-8') as f:
        csv_keyword = csv.reader(f)
        keywords = []
        for line in csv_keyword:
            if '+' not in line[0]:
                keywords.append(line)
    return keywords


for i in range(len(pathDir1)):

    newdir = pathDir1[i][0:-11] # 目标文件名，用list列表切片方法修改文件名
    keywords_in1 = get_keywords(filenames_in1, pathDir1[i])  # (目录，目录下的第i个file)
    #keywords_in2 = get_keywords(filenames_in2, pathDir2[i])
    keywords_all = []
    #result1 = []
    result2 = []

    # 将关键词与NER结果比对，删除包含关系中的子词
    #for keyword in keywords_in2:  # 遍历每一行
    #    count = 0
    #    for keywordsNER in keywords_in1:
    #        keywordsNER[0] = keywordsNER[0].upper()
    #        matchObj = re.search(str(keyword[0]),str(keywordsNER[0]))
    #        if matchObj!=None:
    #            count=count+1
    #    if count ==0:
    #        result1.append(keyword)  # 整行加入result

    # 将NER与NER结果比对，删除包含关系中的子词
    for keyword in keywords_in1:  # 遍历每一行
        count = 0
        for keywordsNER in keywords_in1:
            if keyword[0] != keywordsNER[0]:    # 判断是否是同一词
                matchObj = re.search(str(keyword[0]),str(keywordsNER[0]))
                if matchObj!=None:
                    count=count+1
        if count == 0:
            result2.append(keyword)  # 整行加入result

    #keywords_all = result1 + result2
    keywords_all = result2

    Excel = open(filenames_out + newdir + "知识点.csv", 'w', newline='', encoding='utf-8')  # 打开表格文件，若表格文件不存在则创建
    writ = csv.writer(Excel)  # 创建一个csv的writer对象用于写每一行内容
    for i in range(len(keywords_all)):
        if len(keywords_all[i][0])>1: # 去掉单字符的词
            writ.writerow(keywords_all[i])  # 将result中所有行写入表格

